package com.news.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.news.common.CommonEnum;
import com.news.common.ResponseBean;
import com.news.common.ResultUtil;
import com.news.common.ToolsUtils;
import com.news.entity.NewsComment;
import com.news.entity.SysNews;
import com.news.entity.SysUser;
import com.news.service.NewsCommentService;
import com.news.service.SysNewsService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 新闻评论表 前端控制器
 * </p>
 *
 */
@Controller
@RequestMapping({"/newsComment","/home/newsComment"})
public class NewsCommentController {

    private static  final Logger logger= LoggerFactory.getLogger(NewsCommentController.class);

    private static final String page_prefix="news/comment/";
    @Autowired
    NewsCommentService commentService;

    @Autowired
    SysNewsService newsService;
    /**
     * 跳转列表页面
     * @return
     */
    @RequestMapping("/listPage")
    public String listPage() {
        return page_prefix+"list";
    }

    /**
     * 添加用户评论
     * @param comment
     * @param session
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public ResponseBean add(NewsComment comment, HttpSession session){
        //变量类型 变量名  =  类/对象  . 方法         方法入参
        SysUser loginUser = ToolsUtils.getLoginUser(session);
        //将loginUser的Id 设置为comment的userId
        comment.setUserId(loginUser.getId());
        comment.setUserName(loginUser.getName());
        comment.setField1(loginUser.getField1());
        Long newsId = comment.getNewsId();
        SysNews news = newsService.getById(newsId);
        comment.setNewsTitle(news==null?"已经被删除":news.getTitle());
        boolean save = commentService.save(comment);
        return save?ResultUtil.successData(comment):ResultUtil.error();
    }
    /**
     * 新闻评论列表查询
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @param queryParam 查询参数
     * @param vagueParam 模糊参数
     * @return
     */
    @ApiOperation("获取新闻评论列表")
    @RequestMapping("/list/{pageNum}/{pageSize}")
    @ResponseBody
    public ResponseBean getList(@PathVariable Integer pageNum,@PathVariable Integer pageSize,
                                NewsComment queryParam,String vagueParam) {
        logger.debug("查询新闻评论列表参数："+queryParam+",pageNum:"+pageNum+",pageSize:"+pageSize);
        //sql  where
        QueryWrapper<NewsComment> queryWrapper=new QueryWrapper<>();
        String newsTitle = queryParam.getNewsTitle();
        String content = queryParam.getContent();
        String userName = queryParam.getUserName();
        //where news_title like '%'+newsTitle+'%'
        queryWrapper.like(!StringUtils.isEmpty(newsTitle),"news_title",newsTitle)
                .like(!StringUtils.isEmpty(content),"content",content)
                .like(!StringUtils.isEmpty(userName),"user_name",userName);
        Long newsId = queryParam.getNewsId();
        //equals ==
        queryWrapper.eq(newsId!=null,"news_id",newsId);
        // order by id desc
        queryWrapper.orderByDesc("id");
        IPage<NewsComment> indexPage = new Page<>(pageNum, pageSize);
        //分页查询
        IPage<NewsComment> listPage = commentService.page(indexPage, queryWrapper);
        logger.debug("获取的新闻评论列表："+listPage);
        //调用工具类对结果进行封装然后返回
        Map resultMap = ToolsUtils.wrapperResult(listPage, "commentList");
        return ResultUtil.successData(resultMap);
    }

    /**
     * 批量删除评论
     * @param idList
     * @return
     */
    @RequestMapping("deleteBatchByIds")
    @ResponseBody
    public ResponseBean deleteBatchByIds(@RequestParam List<Long> idList){
        logger.debug("deleteBatchByIds:"+idList);
        if(idList==null||idList.isEmpty()){
            return ResultUtil.error(CommonEnum.BAD_PARAM);
        }
        boolean delete = commentService.removeByIds(idList);
        logger.debug("批量删除结果："+delete);
        return ResultUtil.success() ;
    }

    @RequestMapping("deleteById")
    @ResponseBody
    public ResponseBean deleteById(@RequestParam Long id){
        if(id==null||id<0){
            return ResultUtil.error(CommonEnum.BAD_PARAM);
        }
        boolean delete = commentService.removeById(id);
        return ResultUtil.success() ;
    }

}
